package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.result.Result;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WorkSpaceServiceImpl
 * @Author: YuanDing
 * @Date: 2024/2/17 17:07
 * @Description:
 */

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishesMapper dishesMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     *
     * @return 工作台今日运营数据
     */
    @Override
    public Result businessData(LocalDate begin , LocalDate end) {
        LocalDateTime beginTime = begin.atStartOfDay();
        LocalDateTime endTime = end.atTime(LocalTime.MAX);
        HashMap<String, LocalDateTime> map = new HashMap<>();
        map.put("begin" , beginTime);
        map.put("end" , endTime);
        Integer newUserNumber = userMapper.selectByMap(map);

        Integer validOrderNumber = reportMapper.validOrderCountByCondition(beginTime, endTime);
        List<Orders> ordersList = reportMapper.selectOrderByCondition(beginTime, endTime);

        double orderCompletionRate = 0;
        if (ordersList.size() != 0) {
            orderCompletionRate = (double) validOrderNumber / ordersList.size();
        }

        Integer turnover = reportMapper.countTurnoverByDay(beginTime, endTime);

        Integer userNumber = orderMapper.countOrderUserByTime(beginTime, endTime);

        double unitPrice = 0;
        if (turnover != null && userNumber != 0) {
            unitPrice = (double)turnover / userNumber;
        }

        return Result.success(BusinessDataVO.builder()
                .turnover(Double.valueOf(turnover == null ? 0 : turnover))
                .unitPrice(unitPrice)
                .validOrderCount(validOrderNumber)
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUserNumber)
                .build());
    }

    @Override
    public Result overviewOrders() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);
        //待接单
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return Result.success(OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build());
    }



    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishesMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishesMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
